package com.chaokang.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chaokang.dao.entity.*;
import com.chaokang.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController

public class AnswerControllerImpl implements AnswerController{
    @Autowired
    private AnswerService answerService;
    private HashMap<String,Integer> isSuccess(int result){
        HashMap<String,Integer> isSuccess = new HashMap<>();
        if(result==1){
            isSuccess.put("success",result);
        }else {
            isSuccess.put("fail", result);
        }
        return isSuccess;
    }

    @PostMapping(value = "/service/getAnswerModel",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerModel(@RequestBody AnswerEntity answerEntity){
        for (AnswerEntity answer:answerService.getAnswerByQuestionId
                (answerEntity.getQuestionnaire_id())) {
            if("模板".equals(answer.getState())){
                return answer;
            }
        }
        return null;
    }
    @PostMapping(value = "/service/getAnswerById",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerById(@RequestBody AnswerEntity answerEntity){
        return answerService.getAnswerById(answerEntity.getId());
    }
    @PostMapping(value = "/service/getAnswerByQuestionId",produces = MediaType.APPLICATION_JSON_VALUE)
    public List<AnswerEntity> getAnswerByQuestionId(@RequestBody AnswerEntity answerEntity){
        return answerService.getAnswerByQuestionId(answerEntity.getQuestionnaire_id());
    }
    @PostMapping(value = "/service/insertAnswer",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String,Integer> insertAnswer(@RequestBody AnswerEntity answerEntity){
        return isSuccess(answerService.insertAnswer(answerEntity));
    }
    @PostMapping(value = "/service/updateAnswer",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String,Integer> updateAnswer(@RequestBody AnswerEntity answerEntity){
        return isSuccess(answerService.updateAnswer(answerEntity));
    }
    @PostMapping(value = "/service/getAnswerByQuestionIdAndUserId",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerByQuestionIdAndUserId(@RequestBody AnswerEntity answerEntity){
        return answerService.getAnswerByQuestionIdAndUserId(answerEntity.getQuestionnaire_id(),answerEntity.getUser_id());
    }
    @PostMapping(value="/service/getAllUserIdByQuestionId",produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Integer> getAllUserIdByQuestionId(@RequestBody AnswerEntity answerEntity) {
        return answerService.getAllUserIdByQuestionId(answerEntity.getQuestionnaire_id());
    }

    @PostMapping(value = "/service/getAnswerDetailByQuestionnaireId",produces = MediaType.APPLICATION_JSON_VALUE)
    public AnswerEntity getAnswerDetailByQuestionnaireId(@RequestBody AnswerEntity answerEntity) {
        List<AnswerEntity> questionnaireDetail =  answerService.getAnswerByQuestionId(answerEntity.getQuestionnaire_id());
        //System.out.println(111);
        QuestionnaireAdapter total = new QuestionnaireAdapter();
        List<QuestionnaireAdapter> questionnaireAdapterList = new ArrayList<>();

        for (AnswerEntity entity : questionnaireDetail) {
            //System.out.println(entity.getAnswer());
            JSONObject jQA = JSONObject.parseObject(entity.getAnswer());
            //QuestionnaireAdapter q = new QuestionnaireAdapter(jo.getObject("question", List.class));
            //questionnaireAdapterList.add(q);
            QuestionnaireAdapter qa = new QuestionnaireAdapter();
            qa.setQuestionnaireAnswer(new ArrayList<>());
            JSONArray jsonArray =  (JSONArray) jQA.get("answer");

            for (Object o : jsonArray) {
                JSONObject qAD =  (JSONObject) o;
                QuestionAnswerDetail qadTemp = new QuestionAnswerDetail();
                if (qAD.get("type").equals("简答")){
                    continue;
                }
                qadTemp.setAnswer(String.valueOf(qAD.get("answer")));
                qadTemp.setTitle(String.valueOf(qAD.get("title")));
                qadTemp.setType(String.valueOf(qAD.get("type")));
                qadTemp.setOptions(new ArrayList<>());
                JSONArray options = (JSONArray) qAD.get("option");
                for (Object op: options) {
                    JSONObject opt = (JSONObject) op;
                    Option option = new Option();
                    option.setValue(String.valueOf(opt.get("value")));
                    if (qadTemp.getAnswer().contains(option.getValue())){
                        option.setCount(1);
                    }
                    qadTemp.getOptions().add(option);
                }
                qa.getQuestionnaireAnswer().add(qadTemp);
                //System.out.println(111);
            }
            questionnaireAdapterList.add(qa);
        }


        total.setQuestionnaireAnswer(questionnaireAdapterList.get(0).getQuestionnaireAnswer());
        int l0 = questionnaireAdapterList.size();
        for (int i=1;i<l0;i++){
            QuestionnaireAdapter questionnaireAdapter = questionnaireAdapterList.get(i);
            int l = questionnaireAdapter.getQuestionnaireAnswer().size();
            for (int j=0;j<l;j++){
                List<Option> options = total.getQuestionnaireAnswer().get(j).getOptions();
                String answer = questionnaireAdapter.getQuestionnaireAnswer().get(j).getAnswer();
                for (Option option: options){
                    if (answer.contains(option.getValue())){
                        option.setCount(option.getCount()+1);
                    }
                }
            }
        }

        String json = JSONObject.toJSONString(total.getQuestionnaireAnswer());
        json = "{\"answers\":" + json + "}";
        answerEntity.setAnswer(json);
        return answerEntity;

    }

    @Override
    public List<AnswerNumberEntity> analysisAnswerData(AnswerEntity answerEntity) {
        return null;
    }
}
